﻿using System;
using System.Collections.Generic;
using System.Data;
using DataBase;
using DBService;

namespace YJKFamilyLibDataManager
{
	public class DbCommentary : DbObject
	{
		public DbUser User
		{
			get
			{
				return this.mUser;
			}
		}

		public DateTime Date
		{
			get
			{
				return this.mDate;
			}
		}

		public string Text
		{
			get
			{
				return this.mText;
			}
		}

		public DbFamily DbFamily
		{
			get
			{
				return this.mDbFamily;
			}
		}

		public DbCommentary(int id, DbUser user, DateTime time, string txt, DbFamily dbFamily, IDatabase db)
		{
			this.mId = id;
			this.mUser = user;
			this.mDate = time;
			this.mText = txt;
			this.mDbFamily = dbFamily;
			this.mDatabase = db;
		}

		public DbCommentary(DbUser user, string txt, DbFamily dbFamily)
		{
			this.mDbFamily = dbFamily;
			this.mUser = user;
			this.mText = txt;
		}

		public static DbCommentary GetDbCommentary(int id, IDatabase db)
		{
			DbCommentary result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("select UserId,Content,FamilyGUID,Date from CommentaryTable where Id={0}", id);
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					DbUser user = DbUser.GetUser(dataTable.Rows[0]["UserId"].ToString().Trim(), db);
					DateTime time = Convert.ToDateTime(dataTable.Rows[0]["Date"].ToString());
					DbFamily dbFamily = ((FamilyLibDatabase)db).FamilyTable[new Guid(Convert.ToString(dataTable.Rows[0]["FamilyGUID"]))];
					result = new DbCommentary(id, user, time, dataTable.Rows[0]["Content"].ToString(), dbFamily, db);
				}
				else
				{
					result = null;
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public static List<DbCommentary> GetByFamily(IDatabase db, DbFamily dbFamily)
		{
			List<DbCommentary> result;
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				List<DbCommentary> list = new List<DbCommentary>();
				string sql = string.Format("select Id,UserId,Content,FamilyGUID,Date from CommentaryTable where FamilyGUID='{0}'", dbFamily.Guid.ToString());
				DataTable dataTable = db.ExecQuery(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						DbUser fromDB = DbUser.GetFromDB(Convert.ToInt32(dataTable.Rows[i]["UserId"]), BimServerDB.Default);
						DateTime time = Convert.ToDateTime(dataTable.Rows[i]["Date"].ToString());
						DbFamily dbFamily2 = ((FamilyLibDatabase)db).FamilyTable[new Guid(Convert.ToString(dataTable.Rows[i]["FamilyGUID"]))];
						int id = Convert.ToInt32(dataTable.Rows[i]["Id"].ToString().Trim());
						list.Add(new DbCommentary(id, fromDB, time, dataTable.Rows[i]["Content"].ToString(), dbFamily2, db));
					}
				}
				result = list;
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("insert into CommentaryTable (FamilyGuid,UserId,UserName,Content,Date) VALUES ('{0}',{1},'{2}','{3}','{4}')", new object[]
				{
					this.mDbFamily.Guid,
					this.mUser.Id,
					this.mUser.Name,
					this.mText,
					DateTime.Now.ToString()
				});
				db.ExecuteCommand(sql);
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM CommentaryTable WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		private DbUser mUser;

		private DateTime mDate;

		private string mText;

		private DbFamily mDbFamily;
	}
}
